#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
商户基本信息查询模型
"""

from typing import Dict, Any, Optional, List
from dataclasses import dataclass, asdict
from jlpay.model.base import OrgBaseReq, BaseResponse
from jlpay.utils.annotation_utils import Decrypt, SensitiveField


class MerchBaseInfoQueryRequest(OrgBaseReq):
    """商户基本信息查询请求参数"""
    
    API_URL = "/open/merch/access/merch-base-info/query"
    
    def __init__(self):
        """初始化商户基本信息查询请求"""
        super().__init__()
        self.merch_no = None
    
    def set_merch_no(self, merch_no: str):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            MerchBaseInfoQueryRequest实例
        """
        self.merch_no = merch_no
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return super().to_dict()


@dataclass
class RateInfoDto:
    """费率信息"""
    fee_type: str = ""  # 费率类型
    fee_rate: str = ""  # 费率值
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return asdict(self)


@Decrypt
class MerchBaseInfoQueryResponse(BaseResponse):
    """商户基本信息查询响应参数"""
    
    def __init__(self, response_dict=None):
        """
        初始化响应对象
        
        Args:
            response_dict: 响应字典
        """
        # 先调用父类初始化，处理ret_code, ret_msg等基础字段
        super().__init__(response_dict)
        
        # 定义需要解密的敏感字段路径（对应Java源码中的@Decrypt注解字段）
        self._sensitive_fields = [
            SensitiveField("id_card_name"),           # 经营者/法人姓名
            SensitiveField("id_card_english_name"),   # 经营者/法人英文名称
            SensitiveField("id_card_no"),             # 经营者/法人证件号码
            SensitiveField("legal_person_name"),      # 法人姓名
            SensitiveField("legal_person_phone"),     # 经营者/法人手机号码
        ]
        
        # 基础信息
        self.merch_no = ""  # 商户号
        self.merch_name = ""  # 商户名称
        self.merch_status = ""  # 商户状态
        self.product_type = ""  # 产品类型
        self.sign_mode = ""  # 协议类型
        self.agent_id = ""  # 服务商编号
        
        # 进件结果
        self.sign_id = ""  # 协议编号
        self.sign_url = ""  # 协议链接
        
        # 经营信息
        self.merch_shortname = ""  # 商户经营名称
        self.merch_english_name = ""  # 商户英文名称
        self.address_code = ""  # 经营地址-省市区
        self.address_detail = ""  # 经营地址-详细地址
        self.mcc_code = ""  # 所属行业
        
        # 结算设置
        self.settle_mode = ""  # 结算方式
        self.day_cut = ""  # 营业周期
        self.account_change_flag = ""  # 是否允许商户自主变更结算账户
        self.postscript = ""  # 结算附言
        self.rate_info = []  # 费率列表
        
        # 辅助证明材料
        self.bank_server_id = ""  # 银行客户经理工号
        self.bank_code = ""  # 银行编号
        
        # 身份信息
        self.merch_type = ""  # 主体类型
        
        # 营业证明信息
        self.license_type = ""  # 执照类型
        self.license_no = ""  # 注册号/统一社会信用代码
        self.cert_number = ""  # 登记证书编号
        self.license_name = ""  # 商户法定名称
        self.business_scope = ""  # 经营范围
        self.license_address = ""  # 注册地址
        self.period_begin = ""  # 有效期限开始日期
        self.period_end = ""  # 有效期限结束日期
        
        # 经营者/法人信息
        self.id_doc_type = ""  # 证件类型
        self.id_card_name = ""  # 经营者/法人姓名 (需要解密)
        self.id_card_english_name = ""  # 经营者/法人英文名称 (需要解密)
        self.id_card_no = ""  # 经营者/法人证件号码 (需要解密)
        self.legal_person_name = ""  # 法人姓名 (需要解密)
        self.legal_person_phone = ""  # 经营者/法人手机号码 (需要解密)
        self.card_period_begin = ""  # 证件有效期开始时间
        self.card_period_end = ""  # 证件有效期结束时间
        
        if response_dict:
            # 从response_dict中提取特定字段，避免与父类字段冲突
            # 基础信息
            self.merch_no = response_dict.get('merch_no', '')
            self.merch_name = response_dict.get('merch_name', '')
            self.merch_status = response_dict.get('merch_status', '')
            self.product_type = response_dict.get('product_type', '')
            self.sign_mode = response_dict.get('sign_mode', '')
            self.agent_id = response_dict.get('agent_id', '')
            
            # 进件结果
            self.sign_id = response_dict.get('sign_id', '')
            self.sign_url = response_dict.get('sign_url', '')
            
            # 经营信息
            self.merch_shortname = response_dict.get('merch_shortname', '')
            self.merch_english_name = response_dict.get('merch_english_name', '')
            self.address_code = response_dict.get('address_code', '')
            self.address_detail = response_dict.get('address_detail', '')
            self.mcc_code = response_dict.get('mcc_code', '')
            
            # 结算设置
            self.settle_mode = response_dict.get('settle_mode', '')
            self.day_cut = response_dict.get('day_cut', '')
            self.account_change_flag = response_dict.get('account_change_flag', '')
            self.postscript = response_dict.get('postscript', '')
            
            # 费率列表
            rate_info_list = response_dict.get('rate_info', [])
            if rate_info_list:
                for rate_data in rate_info_list:
                    if isinstance(rate_data, dict):
                        self.rate_info.append(RateInfoDto(**rate_data))
                    else:
                        self.rate_info.append(rate_data)
            
            # 辅助证明材料
            self.bank_server_id = response_dict.get('bank_server_id', '')
            self.bank_code = response_dict.get('bank_code', '')
            
            # 身份信息
            self.merch_type = response_dict.get('merch_type', '')
            
            # 营业证明信息
            self.license_type = response_dict.get('license_type', '')
            self.license_no = response_dict.get('license_no', '')
            self.cert_number = response_dict.get('cert_number', '')
            self.license_name = response_dict.get('license_name', '')
            self.business_scope = response_dict.get('business_scope', '')
            self.license_address = response_dict.get('license_address', '')
            self.period_begin = response_dict.get('period_begin', '')
            self.period_end = response_dict.get('period_end', '')
            
            # 经营者/法人信息
            self.id_doc_type = response_dict.get('id_doc_type', '')
            self.id_card_name = response_dict.get('id_card_name', '')
            self.id_card_english_name = response_dict.get('id_card_english_name', '')
            self.id_card_no = response_dict.get('id_card_no', '')
            self.legal_person_name = response_dict.get('legal_person_name', '')
            self.legal_person_phone = response_dict.get('legal_person_phone', '')
            self.card_period_begin = response_dict.get('card_period_begin', '')
            self.card_period_end = response_dict.get('card_period_end', '')
    
    def get_sensitive_fields(self):
        """
        返回需要解密的敏感字段路径列表
        
        Returns:
            包含敏感字段路径的列表
        """
        return self._sensitive_fields 